package easier.window.dialog.bottom.impl;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import easier.window.dialog.bottom.BottomDialog;
import easier.window.dialog.bottom.BottomDialogFragment;
import easier.window.window.R;
import java.util.List;

/**
 * 底部弹出可选择的选择框,需要子类创建布局界面显示选项
 * 1.LinearLayoutManager
 * 2.拥有一个取消按钮
 * 3.只能自己配置选项界面,并且绑定数据
 */
public abstract class BottomChooseDialogFragment< T > extends BottomDialogFragment {

    //需要显示的数据
    private List< T > mModels;

    public void setModels( List< T > models ) {

        mModels = models;
    }

    public List< T > getModels() {

        return mModels;
    }

    @Override
    protected Dialog createDialog( Context context, @Nullable Bundle savedInstanceState ) {

        BottomDialog bottomDialog = new BottomDialog( getActivity() ) {
            @Override
            protected void configWindow( Window window ) {

                /* gravity为BOTTOM */
                /* 布局原因:需要将高度设置为match_parent
                 * 1.小数据量的时候recycler靠下显示
                 * 2.大数据量的时候recycler最多只占用50%高度 */
                WindowManager.LayoutParams layoutParams = window.getAttributes();
                layoutParams.gravity = Gravity.BOTTOM;
                layoutParams.width = LayoutParams.MATCH_PARENT;
                layoutParams.height = LayoutParams.MATCH_PARENT;
                window.setAttributes( layoutParams );
            }
        };

        View view = LayoutInflater.from( context ).inflate( R.layout.dialog_choose, null );
        setDialogView( view );

        //显示数据
        RecyclerView recycler = view.findViewById( R.id.recycler );
        //取消按钮
        View cancel = view.findViewById( R.id.close );
        if ( cancel != null ) {
            cancel.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    onCancel();
                    dismiss();
                }
            } );
        }
        recycler.setLayoutManager( new LinearLayoutManager( getContext() ) );
        recycler.setAdapter( new Adapter( mModels ) );

        return bottomDialog;
    }


    /**
     * dialog消失的回调
     */
    protected void onCancel() {}

    /**
     * @param item 选项选中的回调
     */
    protected void onItemChecked( T item ) {}

    /**
     * @return 创建布局
     */
    protected abstract View onCreateItemView( ViewGroup parent );

    /**
     * 绑定数据
     */
    protected void onItemViewBind( View itemView, T item, int position ) {}

    /* 创建界面 */
    protected class Adapter extends RecyclerView.Adapter< Holder > {

        private final List< T > items;

        public Adapter( List< T > items ) {

            this.items = items;
        }

        @NonNull
        @Override
        public Holder onCreateViewHolder( @NonNull ViewGroup parent, int viewType ) {

            View view = onCreateItemView( parent );
            return new Holder( view );
        }

        @Override
        public void onBindViewHolder( @NonNull Holder holder, int position ) {

            holder.bind( items.get( position ), position );
        }

        @Override
        public int getItemCount() {

            return items.size();
        }
    }

    protected class Holder extends RecyclerView.ViewHolder {

        public Holder( @NonNull View itemView ) {

            super( itemView );
            itemView.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick( View v ) {

                    @SuppressWarnings( "unchecked" )
                    T item = (T) itemView.getTag();
                    onItemChecked( item );
                    dismiss();
                }
            } );
        }

        private void bind( T item, int position ) {

            itemView.setTag( item );
            onItemViewBind( itemView, item, position );
        }
    }
}
